/**
 * 代理选择器
 */
(function (vue, $, name) {
  // 注册机构树组件
  vue.component(name, {
    template: '<el-popover v-model="visiable"  @hide="hidenFn" @show="showFn" :width="width" :visible-arrow="false"\
            popper-class="org-popover" trigger="click">\
            <yu-tree ref="agentTree" :data="agentData" show-checkbox node-key="pk" \
             default-expand-all  :props="defaultProps"  @check-change="checkChange"  :default-checked-keys="defaultSelectKeys" \
             ></yu-tree>\
            <div style="float: right;margin-right: 15px"  >\
            <el-button @click="clearFn" size="small">取 消</el-button>\
            <el-button type="primary" @click="confirmFn" size="small">确 定</el-button>\
            </div>\
            <el-input  ref="agentTreeInput"   v-model="selectedVal" readonly :placeholder="placeholder" :disabled="disabled"\
             name="代理集群" slot="reference"   ></el-input>\
            </el-popover>',

    props: {

      size: String,
      value: {
        required: true
      },
      agentName:String,
      params: Array
    },
    data: function () {
      return this.createData();
    },


    // 挂载后
    mounted: function () {

      //alert( this.agentName);
      var _this = this;
     // _this.selectedVal = _this.agentName;
      yufp.service.request({
        method: 'GET',
        url: 'api/use/plats/server/queryServerModuleForCA',
        callback: function (code, message, response) {

          if (code == 0) {
            _this.agentData = response.data;
          }
        }
      });

      this.updateWidth();
    },
    methods: {

      hidenFn:function(){

      },
      showFn:function(){

        const checkedNodes = this.$refs.agentTree.getCheckedNodes();
        for(let i=0;i<checkedNodes.length;i++){
          this.$refs.agentTree.setChecked(checkedNodes[i],false);
        }

        for(let j=0;j<this.params.length;j++){
          this.$refs.agentTree.setChecked(this.params[j].agentId,true,false);
        }

        if(this.params==0&&this.value!=''){
          this.$refs.agentTree.setChecked(this.value,true,true);
        }

      },

      checkChange:function(nodedata,selected){


        if(selected){
          const checkedNodes = this.$refs.agentTree.getCheckedNodes();
          for(let i=0;i<checkedNodes.length;i++){
            if(nodedata.serverType ==='C'){ //选中的是中心节点

              if(checkedNodes[i].serverType==='C'){
                if(checkedNodes[i].serverId!=nodedata.serverId){
                  this.$refs.agentTree.setChecked(checkedNodes[i],false);
                }
              }else{
                if(checkedNodes[i].parentId!=nodedata.serverId ){
                  this.$refs.agentTree.setChecked(checkedNodes[i],false);
                }
              }

            }else{

              if(checkedNodes[i].serverType==='C'){
                if(checkedNodes[i].serverId!=nodedata.parentId){
                  this.$refs.agentTree.setChecked(checkedNodes[i],false);
                }
              }else{
                if(nodedata.parentId!=checkedNodes[i].parentId){
                  this.$refs.agentTree.setChecked(checkedNodes[i],false);
                }
              }
            }

          }

        }


      },


      updateWidth: function () {
        if (this.$refs.agentTreeInput != null && this.$refs.agentTreeInput != undefined) {
          this.width = this.$refs.agentTreeInput.$el.scrollWidth;
        }
      },


      clearFn: function () {
        this.visiable = false;
      },


      confirmFn: function () {


        var _this = this;
        const checkedNodes = this.$refs.agentTree.getCheckedNodes();
         let serverId='';

        let isAll = false;
        for(let i=0;i<checkedNodes.length;i++){
          if(checkedNodes[i].serverType==='C'){
            isAll = true;
            _this.selectedVal=checkedNodes[i].serverName;
            serverId = checkedNodes[i].serverId;
            break;
          }
        }

        if(!isAll&&checkedNodes.length>0){
          _this.selectedVal=checkedNodes[0].parentName;
          serverId = checkedNodes[0].parentId;
        }


        this.$emit('input', serverId );
        _this.$emit('select-fn', checkedNodes);
        _this.visiable = false;
      },


      createData: function () {
        var _this = this;
        var temp = _this.getDefaultData();
        // 深度拷贝
        yufp.extend(true, temp, _this.params);
        return temp;
      },


      getCurrentServerId:function(serverId){

        for(let i=0;i<this.agentData.length;i++){
          if(this.agentData[i].serverId===serverId){
            return this.agentData[i].serverName;
          }
        }

      },

      getDefaultData: function () {
        return {
          defaultSelectKeys:[],
          placeholder: '请选择调度引擎和集群代理',
          selectedVal:"",
          width: 200,
          height: 300,
          visiable: false,
          disabled: false,
          expandLevel: 2, // 默认展开层级
          defaultExpandAll: true,
          dataUrl: 'api/use/plats/server/queryServerModuleForCA',
          agentData:[],
          defaultProps: {
            children: 'children',
            label: 'serverName',
            id:"serverId"
          }
        };
      }
    },
    watch: {
      value:function(val){
        this.selectedVal = this.getCurrentServerId(val);
      },

      agentName: function (val) {
        this.selectedVal = val;
      },

    }
  });
}(Vue, yufp.$, 'yufp-agent-select'));